package com.example.shopping.utils;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.example.shopping.R;

//修改个人信息的对话框
public class EditUserDialog extends Dialog  implements View.OnClickListener {
    EditText et;
    Button ensureBtn,canceBtn;
    public  interface  OnEnsureListener{
        public void  onEnsure(String msg);
    }
    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public EditUserDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit_userinfo);
        et=findViewById(R.id.dialog_et_info);
        ensureBtn=findViewById(R.id.dialog_btn_ensure);
        canceBtn=findViewById(R.id.dialog_btn_cancel);
        ensureBtn.setOnClickListener(this);
        canceBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_btn_cancel:
                cancel();
                break;
            case R.id.dialog_btn_ensure:
                if (onEnsureListener!=null){
                    String msg =et.getText().toString();
                    onEnsureListener.onEnsure(msg);
                }
                break;
        }

    }
    public  void setTipHint(String tipHint){
        et.setHint(tipHint);
    }
    public void setDialogWidth(){
        Window window=getWindow();
        WindowManager.LayoutParams wlp=window.getAttributes();
        Display d=window.getWindowManager().getDefaultDisplay();
        wlp.width=(int)(d.getWidth());
        wlp.gravity= Gravity.BOTTOM;
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);

        handler.sendEmptyMessageDelayed(1,100);

    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            InputMethodManager manager= (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);

        }
    };
}
